//
//  DrawerViewController.m
//  TT
//
//  Created by lanou3g on 16/3/10.
//  Copyright © 2016年 Super磊. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if (!self.tap) {
        self.tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate =self;
        [self.view addGestureRecognizer:self.tap];
    }
    
}


//设置中间视图控制器
-(void)setRootViewController:(UIViewController *)viewController {

    if (!viewController) {
        return;
    }
    //移除子视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC =viewController;
    [self addChildViewController:viewController];
    [self.view addSubview:viewController.view];
    //移除左视图控制器
    [self.leftVC removeFromParentViewController];
    [self.leftVC.view removeFromSuperview];
//    NSLog(@"%@",self.view.subviews);
    //设置导航栏
    [self setBarItem];
}


//展示中间视图(关闭抽屉)
-(void)showRootViewController:(UIViewController *)viewController {

    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame =CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
    //延迟0.4秒
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.leftVC removeFromParentViewController];
        [self.leftVC.view removeFromSuperview];
        [self.rightVC removeFromParentViewController];
        [self.rightVC.view removeFromSuperview];
    });
    [self.tap setEnabled:NO];
    self.rootVC.view.userInteractionEnabled =YES;

}

//打开抽屉
-(void)showleftViewController:(UIViewController *)leftViewController {
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        [self addChildViewController:self.leftVC];
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame =CGRectMake(335, 0, kScreenWidth, kScreenHeight);
    }];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled =NO;

}
-(void)showRightViewController:(UIViewController *)rightViewController {

    if (![self.view.subviews containsObject:self.rightVC.view]) {
        [self addChildViewController:self.rightVC];
        [self.view insertSubview:self.rightVC.view atIndex:0];
        [self.leftVC removeFromParentViewController];
        [self.leftVC.view removeFromSuperview];

    }
    
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame =CGRectMake(-300, 0, kScreenWidth, kScreenHeight);
    }];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled =NO;
}

//设置左上角右上角按钮
-(void)setBarItem {
    UINavigationController *na =(UINavigationController *)self.rootVC;
    UIViewController *vc =na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem =({UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithTitle:@"左" style:(UIBarButtonItemStyleDone) target:self action:@selector(leftAction)];
        item;
    });
    vc.navigationItem.rightBarButtonItem =({UIBarButtonItem *item1 =[[UIBarButtonItem alloc]initWithTitle:@"右" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
        item1;
    });
}
//打开抽屉
-(void)leftAction {
    [self showleftViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}
-(void)rightAction {
    [self showRightViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}

#pragma mark -tap点击事件 关闭抽屉 代理方法
-(void)tapAction {

    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
    self.rootVC.view.userInteractionEnabled =YES;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.rootVC.view]);
    }
    return YES;

}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
